import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: {
      title: '注册',
      requiresAuth: false
    }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    redirect: () => {
      const role = store.getters.userRole
      if (role === 'ADMIN') return '/admin'
      if (role === 'TEACHER') return '/teacher'
      return '/student'
    },
    meta: {
      title: '仪表板',
      requiresAuth: true
    }
  },
  {
    path: '/student',
    name: 'Student',
    component: () => import('@/views/Student/Index.vue'),
    meta: {
      title: '学生中心',
      requiresAuth: true,
      roles: ['STUDENT', 'TEACHER', 'ADMIN']
    }
  },
  {
    path: '/student/listening',
    name: 'StudentListening',
    component: () => import('@/views/Student/ListeningPractice.vue'),
    meta: {
      title: '听力练习',
      requiresAuth: true,
      roles: ['STUDENT', 'TEACHER', 'ADMIN']
    }
  },
  {
    path: '/student/listening/:materialId',
    name: 'ListeningExercise',
    component: () => import('@/views/Student/ListeningExercise.vue'),
    meta: {
      title: '听力答题',
      requiresAuth: true,
      roles: ['STUDENT', 'TEACHER', 'ADMIN']
    }
  },
  {
    path: '/student/listening/submissions',
    name: 'StudentListeningSubmissions',
    component: () => import('@/views/Student/ListeningSubmissions.vue'),
    meta: {
      title: '听力练习记录',
      requiresAuth: true,
      roles: ['STUDENT']
    }
  },
  {
    path: '/student/reading',
    name: 'StudentReading',
    component: () => import('@/views/Student/ReadingPractice.vue'),
    meta: {
      title: '阅读练习',
      requiresAuth: true,
      roles: ['STUDENT', 'TEACHER', 'ADMIN']
    }
  },
  {
    path: '/student/reading/practice',
    name: 'ReadingExercise',
    component: () => import('@/views/Student/ReadingExercise.vue'),
    meta: {
      title: '阅读答题',
      requiresAuth: true,
      roles: ['STUDENT', 'TEACHER', 'ADMIN']
    }
  },
  {
    path: '/student/writing',
    name: 'StudentWriting',
    component: () => import('@/views/Student/WritingPractice.vue'),
    meta: {
      title: '写作练习',
      requiresAuth: true,
      roles: ['STUDENT', 'TEACHER', 'ADMIN']
    }
  },
  {
    path: '/student/writing/exercise',
    name: 'WritingExercise',
    component: () => import('@/views/Student/WritingExercise.vue'),
    meta: {
      title: '写作练习',
      requiresAuth: true,
      roles: ['STUDENT', 'TEACHER', 'ADMIN']
    }
  },
  {
    path: '/student/grading/results',
    name: 'StudentGradingResults',
    component: () => import('@/views/Student/GradingResults.vue'),
    meta: {
      title: '批改结果',
      requiresAuth: true,
      roles: ['STUDENT']
    }
  },
  {
    path: '/teacher',
    name: 'Teacher',
    component: () => import('@/views/Teacher/Index.vue'),
    meta: {
      title: '教师中心',
      requiresAuth: true,
      roles: ['TEACHER', 'ADMIN']
    }
  },
  {
    path: '/teacher/students',
    name: 'TeacherStudentManagement',
    component: () => import('@/views/Teacher/StudentManagement.vue'),
    meta: {
      title: '学生管理',
      requiresAuth: true,
      roles: ['TEACHER', 'ADMIN']
    }
  },
  {
    path: '/teacher/grading',
    name: 'TeacherGrading',
    component: () => import('@/views/Teacher/Grading.vue'),
    meta: {
      title: '作业批改',
      requiresAuth: true,
      roles: ['TEACHER', 'ADMIN']
    }
  },
  {
    path: '/teacher/listening-grading',
    name: 'TeacherListeningGrading',
    component: () => import('@/views/Teacher/ListeningGrading.vue'),
    meta: {
      title: '听力练习批改',
      requiresAuth: true,
      roles: ['TEACHER', 'ADMIN']
    }
  },
  {
    path: '/teacher/speaking-grading',
    name: 'TeacherSpeakingGrading',
    component: () => import('@/views/Teacher/SpeakingGrading.vue'),
    meta: {
      title: '口语练习批改',
      requiresAuth: true,
      roles: ['TEACHER', 'ADMIN']
    }
  },
  {
    path: '/student/teacher',
    name: 'StudentTeacherSelection',
    component: () => import('@/views/Student/TeacherSelection.vue'),
    meta: {
      title: '选择老师',
      requiresAuth: true,
      roles: ['STUDENT']
    }
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('@/views/Admin/Index.vue'),
    meta: {
      title: '管理中心',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/reading',
    name: 'AdminReading',
    component: () => import('@/views/Admin/ReadingManagement.vue'),
    meta: {
      title: '阅读材料管理',
      requiresAuth: true,
      roles: ['ADMIN', 'TEACHER']
    }
  },
  {
    path: '/admin/reading-practice',
    name: 'AdminReadingPractice',
    component: () => import('@/views/Admin/ReadingPracticeManagement.vue'),
    meta: {
      title: '阅读练习管理',
      requiresAuth: true,
      roles: ['ADMIN', 'TEACHER']
    }
  },
  {
    path: '/admin/writing',
    name: 'AdminWriting',
    component: () => import('@/views/Admin/WritingManagement.vue'),
    meta: {
      title: '写作管理',
      requiresAuth: true,
      roles: ['ADMIN', 'TEACHER']
    }
  },
  {
    path: '/admin/exam',
    name: 'AdminExamManagement',
    component: () => import('@/views/Admin/ExamManagement.vue'),
    meta: {
      title: '考试管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/users',
    name: 'AdminUserManagement',
    component: () => import('@/views/Admin/UserManagement.vue'),
    meta: {
      title: '用户管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/permissions',
    name: 'AdminPermissionManagement',
    component: () => import('@/views/Admin/PermissionManagement.vue'),
    meta: {
      title: '权限管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/listening',
    name: 'AdminListeningManagement',
    component: () => import('@/views/Admin/ListeningManagement.vue'),
    meta: {
      title: '听力材料管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/listening/:materialId/sections',
    name: 'AdminListeningSectionManagement',
    component: () => import('@/views/Admin/ListeningSectionManagement.vue'),
    meta: {
      title: '听力部分管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/listening/:materialId/sections/:sectionId/questions',
    name: 'AdminListeningQuestionManagement',
    component: () => import('@/views/Admin/ListeningQuestionManagement.vue'),
    meta: {
      title: '听力题目管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/speaking',
    name: 'AdminSpeakingManagement',
    component: () => import('@/views/Admin/SpeakingManagement.vue'),
    meta: {
      title: '口语练习管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/carousels',
    name: 'AdminCarouselManagement',
    component: () => import('@/views/Admin/CarouselManagement.vue'),
    meta: {
      title: '轮播图管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/admin/illustrations',
    name: 'AdminIllustrationManagement',
    component: () => import('@/views/Admin/IllustrationManagement.vue'),
    meta: {
      title: '插图管理',
      requiresAuth: true,
      roles: ['ADMIN']
    }
  },
  {
    path: '/student/speaking',
    name: 'StudentSpeaking',
    component: () => import('@/views/Student/SpeakingPractice.vue'),
    meta: {
      title: '口语练习',
      requiresAuth: true,
      roles: ['STUDENT']
    }
  },
  {
    path: '/student/speaking/:practiceId',
    name: 'SpeakingExercise',
    component: () => import('@/views/Student/SpeakingExercise.vue'),
    meta: {
      title: '口语练习',
      requiresAuth: true,
      roles: ['STUDENT']
    }
  },
  {
    path: '/student/exam',
    name: 'StudentExamList',
    component: () => import('@/views/Student/ExamList.vue'),
    meta: {
      title: '考试列表',
      requiresAuth: true,
      roles: ['STUDENT']
    }
  },
  {
    path: '/student/exam/:examId',
    name: 'StudentExamTaking',
    component: () => import('@/views/Student/ExamTaking.vue'),
    meta: {
      title: '考试答题',
      requiresAuth: true,
      roles: ['STUDENT']
    }
  },
  {
    path: '/student/exam/:examId/v2',
    name: 'StudentExamRunner',
    component: () => import('@/views/Student/ExamTaking.vue'),
    meta: {
      title: '考试答题（统一试卷）',
      requiresAuth: true,
      roles: ['STUDENT']
    }
  },
  {
    path: '/teacher/exam',
    name: 'TeacherExamGrading',
    component: () => import('@/views/Teacher/ExamGrading.vue'),
    meta: {
      title: '考试批改',
      requiresAuth: true,
      roles: ['TEACHER', 'ADMIN']
    }
  },

  {
    path: '/debug/api',
    name: 'ApiTest',
    component: () => import('@/views/Debug/ApiTest.vue'),
    meta: {
      title: 'API调试',
      requiresAuth: true
    }
  },
  {
    path: '/debug/user-status',
    name: 'UserStatus',
    component: () => import('@/views/Debug/UserStatus.vue'),
    meta: {
      title: '用户状态调试',
      requiresAuth: true
    }
  },
  {
    path: '/debug/carousel-test',
    name: 'CarouselTest',
    component: () => import('@/views/Debug/CarouselTest.vue'),
    meta: {
      title: '轮播图功能测试',
      requiresAuth: true
    }
  },
  {
    path: '/test/reading',
    name: 'SimpleReadingTest',
    component: () => import('@/views/Test/SimpleReadingTest.vue'),
    meta: {
      title: '简单阅读测试',
      requiresAuth: true
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    component: () => import('@/views/404.vue'),
    meta: {
      title: '页面不存在'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = `${to.meta.title} - 雅思备考网站`
  
  const token = store.getters.token
  const userRole = store.getters.userRole
  
  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    if (!token) {
      // 未登录，跳转到登录页
      next('/login')
      return
    }
    
    // 检查角色权限
    if (to.meta.roles && !to.meta.roles.includes(userRole)) {
      // 没有权限，跳转到仪表板
      next('/dashboard')
      return
    }
  }
  
  // 如果已登录且访问注册页，跳转到仪表板
  // 但允许访问登录页（用户可能想要重新登录）
  if (token && to.path === '/register') {
    next('/dashboard')
    return
  }
  
  next()
})

export default router
